package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.controller.user.ShoppingCartController;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(ShoppingCartDTO shoppingCartDTO) {

        //先判断是否存在
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);

        Long userid = BaseContext.getCurrentId();
        shoppingCart.setUserId(userid);

        //每个用户都有一个自己的购物车,可以通过user_id区分购物车；
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);

        //若存在就将数量加一
        if (list != null && list.size() > 0) {
            ShoppingCart cart = list.get(0);
            cart.setNumber(cart.getNumber() + 1);
            shoppingCartMapper.updateNumber(cart);
        } else {
            //若不存在就插入
            Long dishID = shoppingCartDTO.getDishId();
            Long setmealID = shoppingCartDTO.getSetmealId();
            if (dishID != null) {
                //此时为菜品
                Dish dish = dishMapper.selectByID(dishID);
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());

            } else {
                //此时为套餐
                Setmeal setmeal = setmealMapper.selectByID(setmealID);
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);

        }
    }

    @Override
    public List<ShoppingCart> list(Long userId) {
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(userId);
        return shoppingCartMapper.list(shoppingCart);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        //先抽取成一个实体类
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        Long userid = BaseContext.getCurrentId();
        shoppingCart.setUserId(userid);

        //每个用户都有一个自己的购物车,可以通过user_id区分购物车；
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);

        ShoppingCart cart = list.get(0);
        if(cart.getNumber()-1>0){
            cart.setNumber(cart.getNumber()-1);
            shoppingCartMapper.updateNumber(cart);
        }else{
            shoppingCartMapper.delete(cart);
        }
    }

    @Override
    public void clean(Long userId){
        List<ShoppingCart> list = shoppingCartMapper.getByUserID(userId);
        if(list != null && list.size()>0){
            shoppingCartMapper.clean(userId);
        }else{
            log.error("当前用户的购物车为空");
        }
    }

}
